package com.dongdongshop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.pojo.vo.GoodsVo;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplateServcie;
import com.dongdongshop.util.PageBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goodsController")
public class GoodsController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Reference(check=false ,timeout=1006666)
    private GoodsService goodsService;
    @Reference(check=false ,timeout=1006666)
    private TemplateServcie templateServcie;
    @Reference(check=false ,timeout=1006666)
    private SpecificationService specificationService;

    //跳转到商品增加页面
    @RequestMapping("goodsList")
    public String goodsList(){
        return "admin/goods_edit";
    }
    //跳转到商品管理页面
    @RequestMapping("goodList")
    public String goodList(){
        return "admin/goods";
    }
    //三级联动，查询商品类目
    @RequestMapping("getDoodsByPid")
    @ResponseBody
    public Result getCityByPid(Integer pid){
        try {
            logger.info("查询方法开始运行{}");
            List<TbItemCat> list = goodsService.getGoodInfo(pid);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //根据模板id获取品牌
    @RequestMapping("getBrandByTypeid")
    @ResponseBody
    public Result getBrandByTypeid(Integer typeId){
        try {
            logger.info("获取品牌方法开始运行{}");
            TbTypeTemplate template = templateServcie.getBrandByTypeid(typeId);
            return Result.response(ResultEnum.SUCCESS).setData(template);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //增加商品信息，设计商品表和商品详细表
    @RequestMapping("insertGoods")
    @ResponseBody
    public Result insertGoods(TbGoods goods, TbGoodsDesc goodsDesc,String itemList){
        try {
            //1、获取subject
            Subject subject = SecurityUtils.getSubject();
            //获取用户session
            TbSeller seller = (TbSeller)subject.getPrincipal();
            goods.setSellerId(seller.getSellerId());

            List<TbItem> list = JSON.parseArray(itemList,TbItem.class);
            logger.info("增加商品方法开始运行{}");
            goodsService.insertGoods(goods,goodsDesc,list);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("getSpecById")
    @ResponseBody
    public Result getSpecById(@RequestParam("specId[]")Integer[] specId){
        try {
            logger.info("获取规格选项方法开始运行{}");
            Map<Integer,List<TbSpecificationOption>> speco = specificationService.getSpecById(specId);
            return Result.response(ResultEnum.SUCCESS).setData(speco);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //获取商品管理的商品信息
    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public Result getGoodsInfo(Integer auditStatus,String goodsName){
        try {
            //1、获取subject
            Subject subject = SecurityUtils.getSubject();
            //获取用户session
            TbSeller seller = (TbSeller)subject.getPrincipal();
            logger.info("获取商品信息方法开始运行{}");
            List<GoodsVo> goodsInfo =goodsService.getGoodsInfo(seller.getSellerId(),auditStatus,goodsName);
            return Result.response(ResultEnum.SUCCESS).setData(goodsInfo);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Long[] ids){
        try {
            logger.info("商品批量删除方法开始执行{}");
            goodsService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //提交审核
    @RequestMapping("SubmitReview")
    @ResponseBody
    public Result SubmitReview(@RequestParam("ids[]")Long[] ids,String statu){
        try {
            logger.info("商品提交审核方法开始执行{}");
            goodsService.SubmitReview(ids,statu);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
